/**
 * @Authoer: luodp
 * @Description: 执行器管理
 * @Date 2018/1/23 14:16
 * @Modified By:
 *
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/taskExecutorManager/taskExecutorManager.js'
], function (require, exports) {
  // page加载完成后调用ready方法
  exports.ready = function (hashCode, data, cite) {
    // 注册该功能要用到的数据字典
    yufp.lookup.reg('TRIGGER_REGTYPE');
    // 创建virtual model
    yufp.custom.vue({
      el: '#triggergroup_grid',
      // 以m_开头的属性为UI数据不作为业务数据，否则为业务数据
      data: function () {
        var me = this;
        return {
          formdata: {},
          height: yufp.custom.viewSize().height - 150,
          createButton: !yufp.session.checkCtrl('addTriggerGroup'), // 新增按钮控制
          editButton: !yufp.session.checkCtrl('editTriggerGroup'), // 修改按钮控制
          deleteButton: !yufp.session.checkCtrl('deleteTriggerGroup'), // 删除按钮控制
          mainGrid: {
            query: {
              title: '',
              appName: '',
              addressType: ''
            },
            height: yufp.custom.viewSize().height - 220,
            dataUrl: backend.jobService + '/api/triggergroup/querypage',
            paging: {
              page: 1,
              size: 10
            },
            currentRow: null,
            tableColumns: [
              {
                label: 'AppName',
                prop: 'appName',
                resizable: true,
                template: function () {
                  return '<template scope="scope">\
                                <a  onclick="return false;" href="javascipt:void(0);" style="text-decoration:underline;" @click="_$event(\'custom-detail-click\', scope)">{{ scope.row.appName }}</a>\
                            </template>';
                }
              },
              { label: this.$t('taskExecutorManager.mz'), prop: 'title', resizable: true },
              { label: this.$t('taskExecutorManager.zcfs'), prop: 'addressType', resizable: true, dataCode: 'TRIGGER_REGTYPE' },
              { label: this.$t('taskExecutorManager.jq'), prop: 'addressList', type: 'date', resizable: true },
              { label: this.$t('taskExecutorManager.sx'), prop: 'orderCode', type: 'date', width: 50 }
            ]
          },

          updateFields: [{
            columnCount: 2,
            fields: [
              {
                field: 'appName',
                label: 'AppName',
                rules: [
                  { required: true, message: this.$t('taskExecutorManager.btx'), trigger: 'blur' },
                  { max: 100, message: this.$t('taskExecutorManager.zdcdbcgghz'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('taskExecutorManager.srxxbhtszf'), trigger: 'blur' }
                ]
              },
              {
                field: 'title',
                label: this.$t('taskExecutorManager.mz'),
                rules: [
                  { required: true, message: this.$t('taskExecutorManager.btx'), trigger: 'blur' },
                  { max: 10, message: this.$t('taskExecutorManager.zdcdbcggzf'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('taskExecutorManager.srxxbhtszf'), trigger: 'blur' }
                ]
              },
              {
                field: 'addressType',
                label: this.$t('taskExecutorManager.zcfs'),
                type: 'select',
                dataCode: 'TRIGGER_REGTYPE',
                disabled: false,
                change: function (addressType) {
                  if (addressType == '1') {
                    me.$refs.otherForm.rebuildFn();
                    me.$refs.otherForm.switch('addressList', 'disabled', false);
                  } else {
                    me.$refs.otherForm.rebuildFn();
                    me.$refs.otherForm.switch('addressList', 'disabled', true);
                  }
                }
              },
              {
                field: 'orderCode',
                label: this.$t('taskExecutorManager.sx'),
                type: 'num',
                rules: [
                  {
                    max: 999,
                    message: '数字(不超过999)',
                    type: 'number',
                    transform: function (value) {
                      return Number(value);
                    }
                  }
                ]
              }
            ]
          }],
          updateOtherFields: [{
            columnCount: 1,
            fields: [
              {
                field: 'addressList',
                label: this.$t('taskExecutorManager.jq'),
                type: 'textarea',
                rows: 1,
                rules: [
                  { max: 200, message: this.$t('taskExecutorManager.zdcdbcgghz'), trigger: 'blur' }
                ]
              }
            ]
          }],
          stsOptions: null,
          dialogFormVisible: false,
          dialogStatus: '',
          formDisabled: false,
          nowRow: null,
          dialogDisabled: true,
          activeName: ['1'], // 默认显示name为1的
          textMap: {
            update: this.$t('taskExecutorManager.xg'),
            detail: this.$t('taskExecutorManager.xq'),
            create: this.$t('taskExecutorManager.xz')
          }
        };
      },
      mounted: function () {
        this.queryFn();
      },
      methods: {
        queryFn: function () {
          var me = this;
          me.$refs.infotable.remoteData();
        },
        openCreateFn: function () {
          this.opType('create', false, ['1', '2']);
          this.updateFields[0].fields[0].readonly = false;
          this.$nextTick(function () {
            this.$refs.form.resetFields();
            this.$refs.otherForm.resetFields();
            this.$refs.form.formModel.addressType = '0';
            this.$refs.form.switch('appName', 'disabled', false);
          });
        },
        openDetailFn: function (row) {
          this.opType('detail', true, ['1', '2']);
          this.$nextTick(function () {
            this.$refs.form.resetFields();
            this.$refs.otherForm.resetFields();
            yufp.extend(this.$refs.form.formModel, row.row);
            yufp.extend(this.$refs.otherForm.formModel, row.row);
          });
        },
        openEditFn: function () {
          if (this.$refs.infotable.selections.length !== 1) {
            this.$message({ message: this.$t('taskExecutorManager.qxzytsj'), type: 'warning' });
            return false;
          }
          this.opType('update', false, ['1', '2']);
          // this.updateFields[0].fields[0].readonly=false;
          var row = this.$refs.infotable.selections[0];
          this.$nextTick(function () {
            this.$refs.form.resetFields();
            this.$refs.otherForm.resetFields();
            yufp.extend(this.$refs.form.formModel, row);
            yufp.extend(this.$refs.otherForm.formModel, row);
            this.$refs.form.switch('appName', 'disabled', true);
          });
        },
        opType: function (type, disabled, active) {
          this.dialogFormVisible = true;
          this.dialogStatus = type;
          this.activeName = active;// 显示哪一块
          this.formDisabled = disabled;
        },
        saveCreateFn: function () {
          var vue = this;
          var form = vue.$refs.form, otherForm = vue.$refs.otherForm;
          var formFlag = true;
          form.validate(function (valid) {
            formFlag = valid;
          });
          if (formFlag) {
            var comitData = {
              addressList: null
            };
            delete form.formModel.id;
            yufp.extend(comitData, form.formModel);
            comitData.addressList = otherForm.formModel.addressList;
            yufp.service.request({
              method: 'POST',
              url: backend.jobService + '/api/triggergroup/',
              data: comitData,
              callback: function (code, message, response) {
                if (code == 0 && response.code == 0) {
                  vue.dialogFormVisible = false;
                  vue.$message({ message: vue.$t('taskExecutorManager.sjbccg') });
                  vue.queryFn();
                } else {
                  vue.$message({ message: response.message, type: 'warning' });
                }
              }
            });
          } else {
            this.$message({ message: this.$t('taskExecutorManager.qjcsrxsfhf'), type: 'warning' });
            return false;
          }
        },
        saveEditFn: function () {
          var me = this;
          var form = me.$refs.form, otherForm = me.$refs.otherForm;
          var formFlag = true;
          form.validate(function (valid) {
            formFlag = valid;
          });
          if (formFlag) {
            var comitData = {
              addressList: null
            };
            yufp.extend(comitData, form.formModel);
            comitData.addressList = otherForm.formModel.addressList;
            yufp.service.request({
              method: 'POST',
              url: backend.jobService + '/api/triggergroup/update',
              data: comitData,
              callback: function (code, message, response) {
                me.dialogFormVisible = false;
                me.$message({ message: me.$t('taskExecutorManager.sjbccg') });
                me.queryFn();
              }
            });
          } else {
            this.$message({ message: this.$t('taskExecutorManager.qjcsrxsfhf'), type: 'warning' });
            return false;
          }
        },
        deletestFn: function () { // 删除
          if (this.$refs.infotable.selections.length == 1) {
            var id = this.$refs.infotable.selections[0].id;
            var _this = this;
            yufp.service.request({
              method: 'POST',
              data: {
                checkId: id
              },
              url: backend.jobService + '/api/triggergroup/checkdelete',
              callback: function (code, message, response) {
                if (response.data == 0) {
                  _this.$confirm(_this.$t('taskExecutorManager.cczjyjscgwjsfjx'), _this.$t('taskExecutorManager.ts'), {
                    confirmButtonText: _this.$t('taskExecutorManager.qr'),
                    cancelButtonText: _this.$t('taskExecutorManager.qx'),
                    type: 'warning',
                    center: true
                  }).then(function () {
                    yufp.service.request({
                      method: 'POST',
                      data: {
                        deleteId: id
                      },
                      url: backend.jobService + '/api/triggergroup/delete',
                      callback: function (code, message, response) {
                        if (code == 0 && response.code == 0) {
                          _this.$message({ message: _this.$t('taskExecutorManager.sccg') });
                          _this.queryFn();
                        } else {
                          _this.$message({ message: response.message, type: 'warning' });
                          return;
                        }
                      }
                    });
                  });
                } else {
                  _this.$message({ message: response.message, type: 'warning' });
                  return;
                }
              }
            });
          } else {
            this.$message({ message: this.$t('taskExecutorManager.qxzytsj'), type: 'warning' });
            return false;
          }
        }
      }
    });
  };

  // 消息处理
  exports.onmessage = function (type, message) {


  };

  // page销毁时触发destroy方法
  exports.destroy = function (id, cite) {

  };
});
